package com.sz.mes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.S;
import com.diboot.core.vo.JsonResult;
import com.sz.mes.dto.EquipmentDTO;
import com.sz.mes.entity.CompleteRpt;
import com.sz.mes.entity.Equipment;
import com.sz.mes.entity.ProductTask;
import com.sz.mes.entity.Tech;
import com.sz.mes.mapper.CompleteRptMapper;
import com.sz.mes.mapper.ProductTaskMapper;
import com.sz.mes.mapper.TechMapper;
import com.sz.mes.service.TechService;
import com.sz.mes.vo.ProductTaskAppVO;
import com.sz.mes.vo.ProductTaskDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 工艺信息查询相关Service实现
 * @author hem
 * @version 1.0
 * @date 2023-08-01
 * Copyright © szhs
 */
@Service
@Slf4j
public class TechServiceImpl extends BaseCustomServiceImpl<TechMapper, Tech> implements TechService {
    private final TechMapper techMapper;
    private final ProductTaskMapper productTaskMapper;
    private final CompleteRptMapper completeRptMapper;
    public TechServiceImpl(TechMapper techMapper, ProductTaskMapper productTaskMapper, CompleteRptMapper completeRptMapper) {
        this.techMapper = techMapper;
        this.productTaskMapper = productTaskMapper;
        this.completeRptMapper = completeRptMapper;
    }

    @Override
    public List<Tech> getList(String taskNum) {
        QueryWrapper<Tech> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_num",taskNum);
        List<Tech> techList = techMapper.selectList(queryWrapper);
        return techList;
    }

    @Override
    public JsonResult getTaskInfo(Equipment equipment) {
        QueryWrapper<Tech> tqw = new QueryWrapper<>();
//        tqw.eq("equipment",equipment.getEquipmentName());
        tqw.eq("tech_name",equipment.getEquipmentType());
        List<Tech> techList = techMapper.selectList(tqw);
        List<String> taskNumList = techList.stream().map(Tech::getTaskNum).collect(Collectors.toList());

        QueryWrapper<ProductTask> pqw = new QueryWrapper<>();
        pqw.in("task_num",taskNumList);
        if (S.isNotBlank(equipment.getTaskNum()) ){
            pqw.and(ew -> ew.like("task_num",equipment.getTaskNum()));
        }else if (S.isNotBlank(equipment.getProductSpecification()) ){
            pqw.and(ew -> ew.like("product_specification",equipment.getProductSpecification()));
        }else if (S.isNotBlank(equipment.getProductNum()) ){
            pqw.and(ew -> ew.like("product_num",equipment.getProductNum()));
        }
        List<ProductTask> taskList = productTaskMapper.selectList(pqw);
        if (taskList == null || taskList.size() == 0){
            return JsonResult.OK(new ArrayList<>());
        }
        Map<String, List<ProductTask>> map = taskList.stream().filter(productTask -> {
            String receiptNum = productTask.getReceiptNum();
            String substring = receiptNum.substring(2);
            String completeRptReceiptNum = "WG" + substring;
            QueryWrapper<CompleteRpt> completeRptQueryWrapper = new QueryWrapper<>();
            completeRptQueryWrapper.eq("receipt_num", completeRptReceiptNum);
            List<CompleteRpt> completeRpts = completeRptMapper.selectList(completeRptQueryWrapper);
            // 只要生产完工表有数据的时候，任务才算已完成
            return completeRpts.size()  == 0;
        }).collect(Collectors.groupingBy(ProductTask::getReceiptNum));
        List<ProductTask> collect = map.values().stream().map(productTasks -> productTasks.get(0)).collect(Collectors.toList());
        return JsonResult.OK(collect);
    }

}
